package com.syezon.wifikey.pwd.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.syezon.wifikey.R;
import com.syezon.wifikey.pwd.info.SaveWifiPwdInfo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/9/10.
 */

public class MyPwdAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String DATA_FORMAT = "yyyy/MM/dd";


    private Context mContext;
    private List<SaveWifiPwdInfo> mData = new ArrayList<>();
    private SimpleDateFormat dateFormat = new SimpleDateFormat(DATA_FORMAT);
    private SavedPwdItemClickListener mListener;

    public MyPwdAdapter(Context context) {
        this.mContext = context;
    }

    public void setListener(SavedPwdItemClickListener listener) {
        if (listener != null) {
            mListener = listener;
        }
    }

    public void setData(List<SaveWifiPwdInfo> data) {
        if (data != null) {
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }

    public List<SaveWifiPwdInfo> getData() {
        return mData;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyPwdViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_save_wifi_pwd_info, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final SaveWifiPwdInfo saveWifiPwdInfo = mData.get(position);
        MyPwdViewHolder viewHolder = (MyPwdViewHolder) holder;
        viewHolder.ivIcon.setImageResource(R.drawable.usual_listicon);
        viewHolder.tvSsid.setText(saveWifiPwdInfo.getSsid());
        viewHolder.tvPwd.setText(saveWifiPwdInfo.getPwd());
        viewHolder.tvTime.setText(dateFormat.format(new Date(saveWifiPwdInfo.getTime())));
        viewHolder.tvConnectTime.setText(String.format("连接%d次", saveWifiPwdInfo.getConnectTime()));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onSavedPwdItemClick(saveWifiPwdInfo);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public static class MyPwdViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_icon)
        ImageView ivIcon;
        @BindView(R.id.tv_ssid)
        TextView tvSsid;
        @BindView(R.id.tv_pwd)
        TextView tvPwd;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_connect_time)
        TextView tvConnectTime;

        public MyPwdViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public static interface SavedPwdItemClickListener {
        void onSavedPwdItemClick(SaveWifiPwdInfo saveWifiPwdInfo);
    }
}
